package com.example.backend.controller;

import com.example.backend.service.SharedBookletService;
import com.example.backend.util.PageUtils;
import com.example.backend.util.Result;
import com.example.backend.util.ResultGenerator;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 共享单词本相关控制器
 * @author 李月
 * @date 2022/5/4 16:46
 */
@RestController
@RequestMapping("/v1/booklets/sharedBooklets")
public class SharedBookletController {
    @Autowired
    private SharedBookletService sharedBookletService;
    /**
     * 分享单词本控制器
     * @param bookletId 要分享的单词本id
     * @return 根据结果返回不同信息
     */
    @PutMapping("/{bookletId}")
    public Result shareBooklet(@PathVariable("bookletId") int bookletId){
        if(sharedBookletService.shareBooklet(bookletId) == 0){
            return ResultGenerator.genSuccessResult("分享成功", null);
        } else if(sharedBookletService.shareBooklet(bookletId) == 1){
            return ResultGenerator.genFailResult("该单词本已分享", null);
        } else{
            return ResultGenerator.genFailResult("分享失败", null);
        }
    }

    /**
     * 查看所有共享单词本控制器
     * @param page 页码
     * @param pageSize 页面大小
     * @return 返回所有共享单词本信息
     */
    @GetMapping
    public Result getSharedBooklets(@Param("page") int page, @Param("pageSize") int pageSize){
        Map<String, Object> map = new HashMap<>();
        if(sharedBookletService.getSharedBooklets(page, pageSize) != null){
            PageUtils p = sharedBookletService.getSharedBooklets(page, pageSize);
            map.put("page", page);
            map.put("pageSize", pageSize);
            map.put("totalPageNum", p.getTotalPage());
            map.put("list", p.getList());
            return ResultGenerator.genSuccessResult("查看所有共享单词本成功", map);
        } else{
            return ResultGenerator.genFailResult("查看所有共享单词本失败", null);
        }
    }

    /**
     * 收藏共享单词本控制器
     * @param bookletId 要收藏的单词本id
     * @param params 参数键值对，包括用户id
     * @return
     */
    @PutMapping("/{bookletId}/collection")
    public Result collection(@PathVariable("bookletId") int bookletId, @RequestBody Map<String, Integer> params){
        if(sharedBookletService.collection(bookletId, params.get("userId")) == 0){
            return ResultGenerator.genSuccessResult("收藏成功", null);
        } else if (sharedBookletService.collection(bookletId, params.get("userId")) == 1){
            return ResultGenerator.genFailResult("该单词本已收藏", null);
        } else {
            return ResultGenerator.genFailResult("收藏失败", null);
        }
    }

    /**
     * 查看共享单词本详情控制器
     * @param bookletId 单词本id
     * @return 共享单词本详情信息
     */
    @GetMapping("/{bookletId}")
    public Result getBookletSpecification(@PathVariable("bookletId") int bookletId){
        if(sharedBookletService.getSharedBookletSpecification(bookletId) != null){
            return ResultGenerator.genSuccessResult("查看共享单词本详情成功", sharedBookletService.getSharedBookletSpecification(bookletId));
        } else {
            return ResultGenerator.genFailResult("查看共享单词本详情失败", null);
        }
    }
}
